package com.xing.servlet;

import com.xing.domain.Book;
import com.xing.service.BookService;
import com.xing.service.impl.BookServiceImpl;
import com.xing.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/*
 * 静，谦虚谨慎，心胸宽广容纳所有好的坏的、先苦后甜
 * day41bookshop
 * 2021/5/6 17:55
 */
@WebServlet(value = "/update")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.解决乱码问题
        req.setCharacterEncoding("utf-8");
        //2.接收数据
        String id = req.getParameter("id");
        String title = req.getParameter("title");
        String author = req.getParameter("author");
        String publicDate = req.getParameter("publicDate");
        String publisher = req.getParameter("publisher");
        String isbn = req.getParameter("isbn");
        String price = req.getParameter("price");
        String picture = req.getParameter("picture");
        String cid = req.getParameter("cid");

        //校验 暂时略
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-mm-dd");
        try {
            Book book=  new Book(Integer.parseInt(id),title,author,sd.parse( publicDate),publisher,isbn,new BigDecimal(price),picture,Integer.parseInt(cid));
            BookService bookService=new BookServiceImpl();
            bookService.modify(book);
            //重定向
            resp.sendRedirect(req.getContextPath()+"/booklists");
        } catch (ParseException e) {
            e.printStackTrace();
            req.setAttribute("msg", "更新失败");
            req.getRequestDispatcher("/message.jsp").forward(req,resp);
            return;

        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
